package net.lab1024.sa.base.module.support.elasticsearch.service;

import lombok.Data;
import net.lab1024.sa.base.common.domain.EventBase;
import net.lab1024.sa.base.module.support.target.EventInfoTarget;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 事件字段配置服务
 *
 * @author chenyuqiang
 * @date 2025-01-27 10:00:00
 */
@Service
public class EventFieldConfigService {

    private final Map<String, FieldConfig> fieldConfigs = new LinkedHashMap<>();

    public EventFieldConfigService() {
        initFieldConfigs();
    }

    /**
     * 获取字段配置
     */
    public Map<String, FieldConfig> getFieldConfigs() {
        return fieldConfigs;
    }

    /**
     * 初始化字段配置
     */
    private void initFieldConfigs() {
        // 通过反射获取所有字段的配置
        for (java.lang.reflect.Field field : EventBase.class.getDeclaredFields()) {
            Field esField = field.getAnnotation(Field.class);
            EventInfoTarget target = field.getAnnotation(EventInfoTarget.class);
            
            if (esField != null && target != null) {
                String fieldName = field.getName();
                String esFieldName = esField.name().isEmpty() ? fieldName : esField.name();
                
                FieldConfig config = new FieldConfig();
                config.setFieldName(esFieldName);
                config.setEntityName(field.getName());
                config.setDisplayName(target.name());
                config.setFieldType(esField.type().name().toLowerCase());
                config.setDescription(target.name());
                
                // 添加字段特定的操作符
                config.setOperators(getOperatorsForFieldType(esField.type()));
                
                fieldConfigs.put(esFieldName, config);
            }
        }
    }

    /**
     * 根据字段类型获取可用的操作符
     */
    private Map<String, String> getOperatorsForFieldType(org.springframework.data.elasticsearch.annotations.FieldType fieldType) {
        Map<String, String> operators = new LinkedHashMap<>();
        
        // 通用操作符
        operators.put(":", "等于");
        operators.put("!=", "不等于");
        operators.put(">", "大于");
        operators.put("<", "小于");
        operators.put(">=", "大于等于");
        operators.put("=<", "小于等于");
        operators.put("exists", "存在");
        operators.put("not exists", "不存在");
        operators.put("in", "在列表中");
        operators.put("not in", "不在列表中");
        operators.put("contains", "包含");
        operators.put("not contains", "不包含");

        // 根据字段类型添加特定操作符
//        switch (fieldType) {
//            case Integer:
//            case Long:
//            case Float:
//            case Double:
//                operators.put(">", "大于");
//                operators.put(">=", "大于等于");
//                operators.put("<", "小于");
//                operators.put("<=", "小于等于");
//                break;
//            case Text:
//            case Keyword:
//                // KQL支持的文本匹配操作符
//                operators.put("*", "通配符匹配");
//                operators.put("~", "模糊匹配");
//                operators.put("contains", "包含");
//                operators.put("not contains", "不包含");
//                break;
//            case Ip:
//                // IP地址专用操作符
//                operators.put("cidr", "CIDR范围匹配");
//                break;
//        }
        
        return operators;
    }

    /**
     * 字段配置类
     */
    @Data
    public static class FieldConfig {
        private String fieldName;
        private String entityName;// ES中的字段名
        private String displayName;      // 显示名称
        private String fieldType;        // 字段类型
        private String description;      // 字段描述
        private Map<String, String> operators = new HashMap<>();  // 可用操作符

    }
} 